我將models內原本FileField的upload_to參數取消讓他儲存到預設的位置
這樣在存入DB的時候才不會顯示資料夾upload
在後面取資料的時候也可以不用再修改檔案名稱
增加以下設定
docsystem_5/settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
doc_info/models.py
class doc_warehouse(models.Model):
upload_file = models.FileField(null=True, blank=True)
要記得執行以下
python manage.py makemigrations #告訴django依據model跟installed_app要改動哪些table
python manage.py migrate #執行以上的變動
python manage.py runserver #執行server
發現另存新檔的時候他會依照content-type & content-disposition的參數去解析檔案
所以要用dict的方式去反應 (dict放最後面以免文章過長)
doc_info/views.py
@login_required
def doc_download(request,doc_id):
user = request.user
Doc_warehouse = doc_warehouse.objects.filter(user_id=user.id)
try:
doc = Doc_warehouse.get(id=doc_id)
upload_file_path = f"media/{str(doc.upload_file)}"
upload_file_extension = str(Path(upload_file_path).suffix).lower()
extexsion_dict = {
...
}
if os.path.exists(upload_file_path):
with open(upload_file_path, 'rb') as file:
response = HttpResponse(file.read(), charset='utf-8' )
response['Content-Type'] = f"{extexsion_dict.get(upload_file_extension,'application/pdf')}"
response['Content-Disposition'] = f"inline; filename={os.path.basename(upload_file_path.encode('utf-8').decode('ISO-8859-1'))}"
return response
except Exception as e:
print(e)
return HttpResponseNotFound('You have no access to this file')
呈現如下
extexsion_dict = {
".epub":"application/epub+zip",
".fif":"application/fractals",
".spl":"application/futuresplash",
".hta":"application/hta",
".hqx":"application/mac-binhex40",
".vsi":"application/ms-vsi",
".accdb":"application/msaccess",
".accda":"application/msaccess.addin",
".accdc":"application/msaccess.cab",
".accde":"application/msaccess.exec",
".accft":"application/msaccess.ftemplate",
".accdr":"application/msaccess.runtime",
".accdt":"application/msaccess.template",
".accdw":"application/msaccess.webapplication",
".one":"application/msonenote",
".doc":"application/msword",
".osdx":"application/opensearchdescription+xml",
".pdf":"application/pdf",
".p10":"application/pkcs10",
".p7c":"application/pkcs7-mime",
".p7s":"application/pkcs7-signature",
".cer":"application/pkix-cert",
".crl":"application/pkix-crl",
".ps":"application/postscript",
".xls":"application/vnd.ms-excel",
".xlsx":"application/vnd.ms-excel.12",
".xlam":"application/vnd.ms-excel.addin.macroEnabled.12",
".xlsb":"application/vnd.ms-excel.sheet.binary.macroEnabled.12",
".xlsm":"application/vnd.ms-excel.sheet.macroEnabled.12",
".xltm":"application/vnd.ms-excel.template.macroEnabled.12",
".thmx":"application/vnd.ms-officetheme",
".sst":"application/vnd.ms-pki.certstore",
".pko":"application/vnd.ms-pki.pko",
".cat":"application/vnd.ms-pki.seccat",
".ppt":"application/vnd.ms-powerpoint",
".pptx":"application/vnd.ms-powerpoint.12",
".ppam":"application/vnd.ms-powerpoint.addin.macroEnabled.12",
".pptm":"application/vnd.ms-powerpoint.presentation.macroEnabled.12",
".sldm":"application/vnd.ms-powerpoint.slide.macroEnabled.12",
".ppsm":"application/vnd.ms-powerpoint.slideshow.macroEnabled.12",
".potm":"application/vnd.ms-powerpoint.template.macroEnabled.12",
".pub":"application/vnd.ms-publisher",
".vsd":"application/vnd.ms-visio.viewer",
".docx":"application/vnd.ms-word.document.12",
".docm":"application/vnd.ms-word.document.macroEnabled.12",
".dotx":"application/vnd.ms-word.template.12",
".dotm":"application/vnd.ms-word.template.macroEnabled.12",
".wpl":"application/vnd.ms-wpl",
".xps":"application/vnd.ms-xpsdocument",
".odp":"application/vnd.oasis.opendocument.presentation",
".ods":"application/vnd.oasis.opendocument.spreadsheet",
".odt":"application/vnd.oasis.opendocument.text",
".pptx":"application/vnd.openxmlformats-officedocument.presentationml.presentation",
".sldx":"application/vnd.openxmlformats-officedocument.presentationml.slide",
".ppsx":"application/vnd.openxmlformats-officedocument.presentationml.slideshow",
".potx":"application/vnd.openxmlformats-officedocument.presentationml.template",
".xlsx":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".xltx":"application/vnd.openxmlformats-officedocument.spreadsheetml.template",
".docx":"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".dotx":"application/vnd.openxmlformats-officedocument.wordprocessingml.template",
".appcontent-ms":"application/windows-appcontent+xml",
".z":"application/x-compress",
".solitairetheme8":"application/x-compressed",
".dtcp-ip":"application/x-dtcp1",
".gz":"application/x-gzip",
".itls":"application/x-itunes-itls",
".itms":"application/x-itunes-itms",
".itpc":"application/x-itunes-itpc",
".jtx":"application/x-jtx+xps",
".latex":"application/x-latex",
".nix":"application/x-mix-transfer",
".asx":"application/x-mplayer2",
".application":"application/x-ms-application",
".vsto":"application/x-ms-vsto",
".wmd":"application/x-ms-wmd",
".wmz":"application/x-ms-wmz",
".xbap":"application/x-ms-xbap",
".website":"application/x-mswebsite",
".p12":"application/x-pkcs12",
".p7b":"application/x-pkcs7-certificates",
".p7r":"application/x-pkcs7-certreqresp",
".pcast":"application/x-podcast",
".swf":"application/x-shockwave-flash",
".sit":"application/x-stuffit",
".tar":"application/x-tar",
".man":"application/x-troff-man",
".asx":"application/x-wmplayer",
".cer":"application/x-x509-ca-cert",
".zip":"application/x-zip-compressed",
".xaml":"application/xaml+xml",
".xht":"application/xhtml+xml",
".xml":"application/xml",
".zip":"application/zip",
".3gp":"audio/3gpp",
".3g2":"audio/3gpp2",
".aac":"audio/aac",
".aiff":"audio/aiff",
".amr":"audio/amr",
".au":"audio/basic",
".ec3":"audio/ec3",
".lpcm":"audio/l16",
".mid":"audio/mid",
".mid":"audio/midi",
".mp3":"audio/mp3",
".m4a":"audio/mp4",
".m4a":"audio/MP4A-LATM",
".mp3":"audio/mpeg",
".m3u":"audio/mpegurl",
".mp3":"audio/mpg",
".adts":"audio/vnd.dlna.adts",
".ac3":"audio/vnd.dolby.dd-raw",
".wav":"audio/wav",
".aiff":"audio/x-aiff",
".flac":"audio/x-flac",
".m4a":"audio/x-m4a",
".m4r":"audio/x-m4r",
".mka":"audio/x-matroska",
".mid":"audio/x-mid",
".mid":"audio/x-midi",
".mp3":"audio/x-mp3",
".mp3":"audio/x-mpeg",
".m3u":"audio/x-mpegurl",
".mp3":"audio/x-mpg",
".wax":"audio/x-ms-wax",
".wma":"audio/x-ms-wma",
".wav":"audio/x-wav",
".dib":"image/bmp",
".gif":"image/gif",
".jpg":"image/jpeg",
".jps":"image/jps",
".mpo":"image/mpo",
".jpg":"image/pjpeg",
".png":"image/png",
".pns":"image/pns",
}
-參考資料: https://www.itread01.com/content/1546929737.html